//
//  AVDSJoinVC.m
//  AVD_Demo
//
//  Created by 3tee on 2022/11/16.
//

#import "AVDJoinVC.h"
#import "AVDSettingVC.h"
#import "AVDMeetingVC.h"
//#import <avd_sdk/avd_sdk.h>
#import "MBProgressHUD.h"

@interface AVDJoinVC ()<AVDEngineDelegate,AVDRoomJoinDelegate>


@property (weak, nonatomic) IBOutlet UITextField *roomIdTf;
@property (weak, nonatomic) IBOutlet UITextField *nameTf;

@end

@implementation AVDJoinVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initAVD];
}

//初始化引擎
- (void)initAVD{
    [[AVDEngine instance] initWithServerUrl: serverUrl accessKey: appKey secretKey:secrKey delegate:self];
    [[AVDEngine instance] setLogParams:@"info debug " file:[self getLogFileName]];
}

- (NSString*)getLogFileName {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd-HHmmss"];
    NSString *destDateString = [dateFormatter stringFromDate: [NSDate date] ];
    NSString *fileName = [NSString stringWithFormat:@"/%@.txt", destDateString];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    docDir = [docDir stringByAppendingString:@"/logs"];
    fileName = [docDir stringByAppendingPathComponent:fileName];
    NSLog(@"getLogFileName fileName:%@", fileName);
   
    return fileName;
    
}

//加入房间事件
- (IBAction)joinRoomClick:(id)sender {
    if(!self.roomIdTf.text.length || !self.nameTf.text.length){
        [self toastMsg:@"房间号或者用户名不能为空"];
        return;
    }
    AVDRoom *room = [AVDRoom obtain:self.roomIdTf.text];
    AVDUser *user = [[AVDUser alloc]initWithUserId:[NSUUID UUID].UUIDString userName:self.nameTf.text userData:@""];
    [room joinWithUser:user delegate:self];
    
}

//创建房间
- (IBAction)creatRoomClick:(id)sender {
    if(!self.roomIdTf.text.length){
        [self toastMsg:@"房间号不能为空"];
        return;
    }
    AVDRoomInfo *roomInfo = [[AVDRoomInfo alloc]initWithRoomId:self.roomIdTf.text roomName:@"test" appRoomId:self.roomIdTf.text ownerId:[NSUUID UUID].UUIDString maxAttendee:100 maxAudio:100 maxVideo:100 roomMode:0 voiceActivated:YES];
    [roomInfo setRoomModeFlag:mcu];
    [[AVDEngine instance] scheduleRoom:roomInfo];
}

//跳转设置界面
- (IBAction)go2setting:(UIButton *)sender {
    AVDSettingVC *settingVC = [AVDSettingVC new];
    settingVC.reInitBlock = ^(NSString *url) {
        [[AVDEngine instance] uninit];
        [[AVDEngine instance]initWithServerUrl:url accessKey:appKey secretKey:secrKey delegate:self];
    };
    [self.navigationController pushViewController:settingVC animated:YES];
}


#pragma mark AVDEngineDelegate
- (void)onInitResult:(AVDResult)result{
    NSString *msg = @"";
    if(result == AVD_Success){
        msg = @"初始化引擎成功";
    }else{
        msg = [NSString stringWithFormat:@"引擎初始化失败 code = %d",result];
    }
    [self toastMsg:msg];
}

- (void)onScheduleRoomResult:(AVDResult)result roomId:(AVDRoomId)roomId{
    NSString *msg = @"";
    if(result == AVD_Success){
        msg = [NSString stringWithFormat:@"房间创建成功; roomid = %d",roomId];
      
    }else{
        msg = [NSString stringWithFormat:@"房间创建失败 code = %d",result];
    }
    [self toastMsg:msg];
}


#pragma mark AVDRoomDelegate
- (void)onJoinResult:(AVDResult)result{
    NSString *msg = @"";
    if(result == AVD_Success){
//        msg = [NSString stringWithFormat:@"加入房间成功"];
        [[NSUserDefaults standardUserDefaults] setValue:self.roomIdTf.text forKey:roomIdKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self.navigationController pushViewController:[AVDMeetingVC new] animated:YES];
        return;
    }else{
        msg = [NSString stringWithFormat:@"加入房间失败 code = %ld",(long)result];
    }
    [self toastMsg:msg];
}

- (void)toastMsg:(NSString *)msg{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
    [hud hideAnimated:YES afterDelay:2.f];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
